/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;


import dto.Positions;
import java.sql.Connection;
import utils.SQLServerDB;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;


/**
 *
 * @author Hai Muoi Thang Sau
 */
public class PositionDAO {

    String SQL_CREATE = "INSERT INTO Positions Values(?,?)";
    String SQL_SEARCHBYUSERNAME = "SELECT * From Positions WHERE Name Like ?";
    String SQL_SEARCHBYID = "SELECT * From Position WHERE ID=?";
    String SQL_READALL = "SELECT * FROM Positions";
    String SQL_UPDATE = "UPDATE Positions Set Name=?, Descript=? WHERE ID =?";
    String SQL_DELETE = "DELETE FROM Positions WHERE ID=?";
    private Connection conn;
    private String lastError;

    public PositionDAO() {
    }

    public boolean create(Positions a) {
        conn = SQLServerDB.getConn();
        try {
            PreparedStatement pr = conn.prepareStatement(SQL_CREATE);
            pr.setString(1, a.getName());
            pr.setFloat(2, a.getSalaryCoefficient());
            if (pr.executeUpdate() > 0) {
                setLastError("Create Successful!");
                return true;
            } else {
                setLastError("Error: Create Failed!");
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return false;
    }

    public ArrayList<Positions> readAll() {
        conn = SQLServerDB.getConn();
        ArrayList<Positions> post = new ArrayList<>();

        try {
            PreparedStatement pr = conn.prepareStatement(SQL_READALL);
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                Positions p = new Positions();
                p.setId(rs.getInt("ID"));
                p.setName(rs.getString("Name"));
                p.setSalaryCoefficient(rs.getFloat("SalaryCoefficients"));
                post.add(p);

            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return post;
    }

    public Positions readById(int id) {
        conn = SQLServerDB.getConn();
        Positions p = new Positions();
        try {
            SQLServerDB db = new SQLServerDB();
            PreparedStatement pr = conn.prepareStatement(SQL_SEARCHBYID);
            pr.setInt(1, id);
            ResultSet rs = pr.executeQuery();
            while (rs.next()) {
                p.setId(rs.getInt("ID"));
                p.setName(rs.getString("Name"));
                p.setSalaryCoefficient(rs.getFloat("SalaryCoefficient"));
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return p;
    }

    public boolean update(Positions a) {
        conn = SQLServerDB.getConn();
        try {
            PreparedStatement pr = conn.prepareStatement(SQL_UPDATE);
            pr.setString(1, a.getName());
            pr.setFloat(2, a.getSalaryCoefficient());
            pr.setInt(3, a.getId());
            if (pr.executeUpdate() > 0) {
                setLastError("Update Successful!");
                return true;
            } else {
                setLastError("Error: Update Failed!");
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return false;
    }

    public boolean delete(int id) {
        conn = SQLServerDB.getConn();
        try {
            PreparedStatement pr = conn.prepareStatement(SQL_DELETE);
            pr.setInt(1, id);
            if (pr.executeUpdate() > 0) {
                setLastError("Delete Successful!");
                return true;
            } else {
                setLastError("Error: Delete Failed!");
            }
        } catch (SQLException ex) {
            setLastError("Error: " + ex.getMessage());
        } finally {
            SQLServerDB.closeConn(conn);
        }
        return false;
    }

    public static void main(String[] args) {
        PositionDAO dao = new PositionDAO();
        ArrayList<Positions> post = dao.readAll();
        Positions a = new Positions();
        a.setName("Tam");
        a.setSalaryCoefficient(5);
        System.out.println(dao.create(a));
        System.out.println(dao.getLastError());

    }

    /**
     * @return the lastError
     */
    public String getLastError() {
        return lastError;
    }

    /**
     * @param lastError the lastError to set
     */
    public void setLastError(String lastError) {
        this.lastError = lastError;
    }

}
